﻿#include "editeventfragment.h"
#include "ui_editeventfragment.h"
#include <QInputDialog>
#include <QMessageBox>
#include <QDebug>
EditEventFragment::EditEventFragment(QDialog *parent) :
    QDialog(parent),
    ui(new Ui::EditEventFragment) {
    ui->setupUi(this);
}

EditEventFragment::~EditEventFragment() {
    delete ui;
}

Event EditEventFragment::getEvent() {
    EditEventFragment ef;
    ef.exec();
    Event ret = ef.event;
    if(ef.isImportant())ret.setImportance(1);
    else ret.setImportance(0);
    return ret;
}

Event EditEventFragment::getEvent(Event e) {
    EditEventFragment ef;
    ef.ui->dateTimeEdit->setTime(e.getTime().time());
    ef.ui->lineEdit_name->setText(e.getName());
    ef.ui->textEdit_description->setText(e.getDescription());
    for(auto x:e.getPeople()) {
        ef.ui->listWidget->addItem(x);
    }
    ef.exec();

    Event ret = ef.event;
    if(ef.isImportant())ret.setImportance(1);
    else ret.setImportance(0);
    return ret;
}

bool EditEventFragment::isImportant() {
    return this->important;
}

void EditEventFragment::on_pushButton_2_clicked() {
    bool ok = false;
    QString name = QInputDialog::getText(this,tr("Input a name"),tr("Name"),QLineEdit::Normal,"",&ok);
    if(ok) {
        ui->listWidget->addItem(name);
    }
}


void EditEventFragment::on_pushButton_clicked() {
    if(ui->lineEdit_name->text()=="") {
        QMessageBox::warning(this,tr("Error"),tr("Event name can't be empty!"));
        return;
    }
    this->event.setName(ui->lineEdit_name->text());
    this->event.setDescription(ui->textEdit_description->toPlainText());
    this->event.setTime(ui->dateTimeEdit->dateTime());
    for(int i = 0; i<ui->listWidget->count(); i++) {
        this->event.addPerson(ui->listWidget ->takeItem(i)->text());
    }
    close();
}


void EditEventFragment::on_checkBox_stateChanged(int arg1) {
    if(arg1!=0)this->event.setImportance(1);
    else this->event.setImportance(0);
}
